$.fn.raty.defaults.starOn = "am-icon-star";
$.fn.raty.defaults.starOff = "am-icon-star-o";
//选择发型师
$(".app-emp-list").on("click", "li", function() {
	$li = $(this);
	$detail = $(".app-emp-detail");
	$("#employeeId").val($li.attr("id"));
	var img = $li.find("img").attr("src");
	if (img != $detail.find("img").attr("src")) {
		$detail.find("img").attr("src", img);
	}
	$detail.find(".em-name").text($li.find(".em-name").text());
	$detail.find(".em-title").text($li.find(".em-title").text());
	$detail.find(".em-intro").text($li.find(".em-intro").text());

	$(".app-emp-list").addClass("am-hide");
	$(".app-emp-detail").removeClass("am-hide");

	initEmployeeSelect($li.attr("id"), $("#shop-sele").val());
});
//重新选择
$(".app-emp-detail").find(".am-btn-default").on("click", function() {
	$(".app-emp-list").removeClass("am-hide");
	$(".app-emp-detail").addClass("am-hide");
	$("#employeeId").val("");
	$("#appointmentTime").val("");
	$("#item-sele").empty();
});
//查看作品
$(".app-emp-detail").find(".am-btn-landiao").on("click", function() {
	employeeWorks($("#employeeId").val());
});
$("#date-modal").on("opened.modal.amui", function() {
	$("#date-modal").css({
		"bottom" : "0",
		"margin-top" : "0"
	});
});

//门店选择事件
$("#shop-sele").change(function() {
	if ($(".app-emp-list").hasClass("am-hide")) {
		$(".app-emp-list").removeClass("am-hide");
		$(".app-emp-detail").addClass("am-hide");
	}
	var shopId = $(this).val();
	initEmployee(shopId);
});

var employeeTemp = "<li id='{4}' class='am-cf'><div class='am-fl'><img src='{0}' onerror='this.src=\"assets/img/avatar.png\"'></div><div class='am-fl'><div class='am-block'><label class='em-name'>{1}</label>"
	+ "<span class='am-text-sm em-title'>{2}</span><span class='am-hide em-intro'>{3}</span></div><div class='am-block raty-div am-text-blue' data-score='{5}'></div></div></li>";
function initEmployee(shopId) {
	var $employeeList = $("#employee-list");
	$employeeList.empty();
	//$("#item-sele").empty();
	$("#employeeId").val("");
	$("#appointmentTime").val("");
	$.ajax({
		type : 'GET',
		url : ctx+"/weixin/vip/shopEmployee/" + shopId,
		dataType : "json",
		success : function(data) {
			$.each(data, function(i, item) {
				$employeeList.append(Comm.format(employeeTemp,
						item.photo == null ? "" : item.photo, item.name,
								item.titleName == null ? "" : item.titleName,
										item.introduction == null ? "" : item.introduction,
												item.employeeId, item.score));
			});
			$(".raty-div").each(function() {
				var vscore = $(this).data("score");
				$(this).raty({
					starType : "i",
					score : vscore,
					readOnly : true
				});
			});
		},
		error : function(ret) {
			Comm.msg("加载失败");
		}
	});
}

//作品
function employeeWorks(employeeId) {
	var $worksList = $("#works-list");
	$worksList.empty();
	$.ajax({
		type : 'GET',
		url : ctx+"/weixin/vip/employeeWorks/" + employeeId,
		dataType : "json",
		success : function(data) {
			if (data.length == 0) {
				Comm.msg("暂无作品");
				return;
			}
			$.each(data, function(i, item) {
				$worksList.append("<li><img src='"+item.photo+"' /></li>");
			});
			$worksList.find("img").on("click", function() {
				wxPreviewImage(this);
			});
			$("#doc-modal").find(".em-name").text(
					$(".app-emp-detail").find(".em-name").text() + "的作品");
			$("#doc-modal").modal();
		},
		error : function(ret) {
			Comm.msg("加载失败");
		}
	});
}

var unOrderTime;
//加载项目和不可预约时间
function initEmployeeSelect(employeeId, shopId) {
	unOrderTime = null;
	var $itemSele = $("#item-sele");
	$itemSele.empty();
	//初始化下拉框
	$.ajax({
		url : ctx+"/weixin/vip/orderEmpSelect",
		data : "employeeId=" + employeeId + "&shopId=" + shopId,
		type : "POST",
		success : function(data) {
			if (data.success) {
				var list = data.params.serviceList;
				if (list.length == 0) {
					$("<option/>").html("该美发师暂无服务项目").val("").appendTo(
					"#item-sele");
					return;
				}
				$(list).each(
						function(n) {
							$("<option/>").html(this.name).val(
									this.serviceItemId).appendTo("#item-sele");
						});
				unOrderTime = data.params;
				$(".date-ul").find("li").eq(0).click();
			}else{
				Comm.msg(data.msg);
				$("#employeeId").val("");
			}
		}
	});
}

//提交
$("#sub-btn").on("click", function() {
	var emploeeId = $("#employeeId").val();
	if (!emploeeId) {
		Comm.msg("请选择美发师");
		return;
	}
	//$("#appointmentTime").val(new Date().format("yyyy-MM-dd HH:mm:ss"));
	var appointmentTime = $("#appointmentTime").val();
	if (!appointmentTime) {
		Comm.msg("请选择预约时间");
		return;
	}
	var itemSele = $("#item-sele").val();
	if (!itemSele) {
		Comm.msg("请选择预约项目");
		return;
	}
	$("#itemName").val($("#item-sele").find("option:selected").text());

	var $btn = $(this);
	$btn.attr("disabled", "disabled").text("正在提交...");
	//$("form").submit();

	$.ajax({
		type : 'POST',
		url : ctx+"/weixin/vip/orderSave",
		dataType : "json",
		data : $('form').serialize(),
		success : function(data) {
			$btn.prop("disabled", false).text("提交");
			Comm.msg(data.msg);
			if (data.success) {
				setTimeout(function() {
					window.location.href = ctx+"/weixin/vip/myOrder";
				}, 1000);
			}
		},
		error : function() {
			Comm.msg("预约失败!");
			$btn.prop("disabled", false).text("提交");
		}
	});
});

//预约时间
var today = new Date();
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate()+1);
var todayStr = today.format("yyyy-MM-dd");
var tomorrowStr = tomorrow.format("yyyy-MM-dd");
var now = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0);
var endDate = new Date().setDate(now.getDate()+90);

$(".date-ul").find("li").eq(0).data("date",todayStr);
$(".date-ul").find("li").eq(1).data("date",tomorrowStr);
initTime(todayStr);

$("#appointmentTime").on("click", function() {
	var emploeeId = $("#employeeId").val();
	if (!emploeeId) {
		Comm.msg("请先选择美发师");
		return;
	}
	$('#date-modal').modal();
});
$("#datepicker").datepicker({
	format : "yyyy-mm-dd",
	onRender: function(date) {
		return (date.valueOf() < now.valueOf()||date.valueOf()>endDate.valueOf()) ? 'am-disabled' : '';
	}
}).on('changeDate.datepicker.amui', function(event) {
	var dateStr = event.date.format("yyyy-MM-dd")
	$(".date-ul").find("li").eq(2).data("date",dateStr);
	initTime(dateStr);
});

//日期
$(".date-ul").on("click", "li", function() {
	$li = $(this);
	$li.addClass("am-active").siblings().removeClass("am-active");
	if($li.hasClass("date-select-li")){//日期控件
		$("#datepicker").parent().removeClass("am-hide");
	}else{
		$("#datepicker").parent().addClass("am-hide");
	}
	initTime($li.data("date"));
});

//时间点
function initTime(date){
	var $time = $(".app-time-group");
	$time.find("li").removeClass("am-disabled");
	$time.find("li").removeClass("am-active");
	$time.find("span").html("&nbsp;");
	$("input:radio[name='tam']").attr("checked",false);

	if(date == undefined){
		return;
	}
	if(date == todayStr){
		$time.find("li").each(function(){
			var hour = $(this).find("input").val();
			var t = new Date((date+" "+hour+":00").replace(/-/g, "/"));
			if(new Date().getTime()<t-advanceNum*60*60*1000){
				$(this).removeClass("am-disabled");
			}else{
				$(this).addClass("am-disabled").find("span").text("已过时");
			}
		});
	}

	if(unOrderTime){
		var empAppTime = this.unOrderTime[date];
		if(empAppTime){
			if(empAppTime.holiday == true){
				//休假
				$time.find("li").each(function(){
					$(this).addClass("am-disabled").find("span").text("休假");
				});
			}else{
				var arr = empAppTime.appointTime;
				for(var i=0;i<arr.length;i++){
					var hour = arr[i];
					$time.find("li").each(function(){
						var $this=$(this);
						if(hour == $this.find("input").val()){
							$this.addClass("am-disabled").find("span").text("已预约");
							return false;
						}
					});
				}
			}
		}
	}
}
//确定按钮
$("#date-btn").on("click", function() {
	var date = $(".date-ul").find("li.am-active").data("date");
	var hour = $("input:radio[name='tam']:checked").val();
	if(!date){
		Comm.msg("请选择日期");
		return;
	}
	if(!hour){
		Comm.msg("请选择时间");
		return;
	}
	var time = date+" "+hour+":00";
	$("#appointmentTime").val(time);
	$('#date-modal').modal('close');
});
initEmployee($("#shop-sele").val());